<?php
/**
* 用户weixin控制器
*/
class Temp extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		$this->load->model('User_model', 'user');
	}
	/**
	 * 用户首页
	 * @DateTime 2017-09-04
     * @author xyl
	 * @return [type] [description]
	 */
	public function index()
	{
		$this->load->view('weixin/index');
	}
	function step()
	{
		$this->load->view('weixin/step');
	}
	function share()
	{
		$this->load->view('weixin/share');
	}
	function record()
	{
		$this->load->view('weixin/record');
	}
	function setting()
	{
		$this->load->view('weixin/setting');
	}
	function credit()
	{
		$this->load->view('weixin/credit');
	}
	function rank()
	{
		$this->load->view('weixin/rank');
	}
	function androidShare()
	{
		$this->load->view('weixin/androidShare');
	}
	function androidInformation()
	{
		$this->load->view('weixin/androidInformation');
	}
	function intro()
	{
		$this->load->view('weixin/intro');
	}
	/**
	 * 添加用户信息
	 * @DateTime 2017-07-06
	 * @author zyl
	 */
	function addUserInfo(){
		if($_POST){
			$post = $this->input->post();
			$post['from'] = 'wx';
			$res = $this->user->userInsert($post);
			if($res){
				redirect(base_url().'Weixin/User/infoSuccess');
			}
			
		}
		$data['user_id'] = $_SESSION['userinfo']['id'];
		// $data['title'] = '信息填写';
    	// $this->load->view('weixin/common/header',$data);
		$this->load->view('weixin/user/addUserInfo',$data);
	}

	/**
	 * 注册提示页
	 * @DateTime 2017-07-13
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function notice(){
		$data['title'] = '提示';

        $this->load->view('weixin/common/header',$data);
		$this->load->view('weixin/login/notice',$data);
	}
	/**
	 * 用户协议
	 * @DateTime 2017-07-07
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function protocol($msg=0){
			
		$this->load->view('weixin/user/protocol');
	}


    /**
     * 手机注册页
     * @DateTime 2017-07-11
     * @author zyl
     * @return   [type]     [description]
     */
    function register(){
         if(isset($_SESSION['userinfo']['status'])){
         	redirect(base_url().'Weixin/User/infoSuccess');
         }
         $this->load->view('weixin/login/register');
    }

    /**
     * 添加微信上传附件
     * @DateTime 2017-08-09
     * @author zyl
     */
    function addWeixinImgs()
    {
    	$post = $this->input->post();

    	$where['id'] = $_SESSION['userinfo']['id'];

    	$data['wx_imgs'] = $post['url'];

    	$return = $this->user->updateAppUser($data,$where);

    	echo json_encode($return);
    }

    /**
     * 上传附件图
     * @DateTime 2017-08-09
     * @author zyl
     * @return   [type]     [description]
     */
	function uploadImg (){
		$where['id'] = $_SESSION['userinfo']['id'];
		if($_POST){
			$post = $this->input->post();
			// print_r($post);die();
			$data['contract_imgs'] = isset($post['contract'])?json_encode($post['contract']):'';
			$data['receipt_imgs'] = isset($post['receipt'])?json_encode($post['receipt']):'';
			if(!empty($post)){
				$res = $this->user->updateAppUser($data,$where);
			}
			redirect(base_url().'Weixin/User/infoSuccess');
		}else{
			//微信设置
	        $this->load->model('Token_model','token');
			$data['jsapi'] = $this->token->getSignPackage();
			
			$user = $this->user->getUser($where);
			$data['receipt'] = json_decode($user['receipt_imgs'],true);
			$data['contract'] = json_decode($user['contract_imgs'],true);
			$this->load->view('weixin/user/upload',$data);
		}
		
	}

    /**
     * 检测用户状态
     * @DateTime 2017-08-23
     * @author zyl
     * @return   [type]     [description]
     */
    function checkUserStatus()
    {
        header("Access-Control-Allow-Origin: *");

        if (isset($_SESSION['userinfo']) && isset($_SESSION['userinfo']['credit_status'])) {
        	$return = array('code'=>1,'msg'=>'已申请');
        } else {
        	$return = array('code'=>0,'msg'=>'未申请');
        }

        echo json_encode($return);
    }


	/**
	 * 信息提交成功界面
	 * @DateTime 2017-07-06
	 * @author zyl
	 * @return   [type]     [description]
	 */
	function infoSuccess(){
        $where['id'] = $_SESSION['userinfo']['id'];
		$user_info = $this->user->getUser($where);
		// $user_info['credit_status'] = 3;
		switch ($user_info['credit_status']) {
			case 2:
				$url = base_url().'Weixin/User/checkFinish';
				break;
			case 3:
				$url = base_url().'Weixin/User/checkInstallment/1';
				break;
			case 1:
				$url = base_url().'Weixin/User/checkInstallment';
				break;
			
			default:
				$url = base_url().'Weixin/User/submitSuccess';
				break;
		}
			redirect($url);
		
		$this->load->view('weixin/user/success');
	}
	
    /**
     * 申请成功
     * @DateTime 2017-08-23
	 * @author xyl
     * @return [type] [description]
     */
	function submitSuccess(){
        $this->load->view("weixin/user/success");
	}
	
	/**
	 * 授信成功
	 * @DateTime 2017-08-23
	 * @author xyl
	 * @return [type] [description]
	 */
	function checkFinish(){
		$where['id'] = $_SESSION['userinfo']['id'];
		$user = $this->user->getUser($where);
		$vdata['info'] = $user;
		$this->load->view('weixin/user/checkFinish',$vdata);
	}

	/**
	 * 审核中
	 * @DateTime 2017-08-16
	 * @author xyl
	 * @param  boolean $type [如果存在则显示重新申请页面，若无则为审核中]
	 * @return [type]        [description]
	 */
	function checkInstallment($type = false)
	{
		if (!empty($type)) {
			$vdata['type'] = 1;
		}
		$where['id'] = $_SESSION['userinfo']['id'];
		$vdata['info'] = $this->user->getUser($where);
		$this->load->view('weixin/user/check',$vdata);
	}

	/**
	 * 重新申请分期
	 * @DateTime 2017-08-23
	 * @author xyl
	 * @return [type] [description]
	 */
	public function resetStatus()
	{
		$res = $this->user->resetStatus();
		echo json_encode($res);return;
	}
}
